// pages/detail/detail.js
const db=wx.cloud.database().collection('apply')
/* const ldb=wx.cloud.database().collection('login') */
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:'',
    _id:'',
    tip:false,
    tip_content:'',
    name:'',
    grade:'',
    role:'',
    objection:false
  },
  btnClick_y:function(){
    const that=this;
    wx.showModal({
      title: '提示',
      content:'是否确定同意该生申请？',
      success:function(res){
        if(res.confirm){
          db.doc(that.data._id).update({
            data:{
              status:'审批成功'
            },
            success:function(res){
              that.tip('审批成功！')
            }
          })
        }
      }
    })
  },
  btnClick_n:function(){
    this.setData({
      objection:true
    })
  },
  cancel:function(){
    this.setData({
      objection:false
    })
  },
  formSubmit(e){
    const that=this;
    if(e.detail.value.bk_reason==''){
      wx.showToast({
        title: '请填写驳回理由',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    
    wx.showModal({
      title: '提示',
      content: '确认信息填写无误吗？',
      success:function(res){
        if(res.confirm){
          var bk_reason=e.detail.value.bk_reason;
          db.doc(that.data._id).update({
            data:{
              bk_reason:bk_reason,
              status:'已驳回'
            },
            success:function(res){
              that.tip('提交成功！')
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that=this
    that.setData({
      role:wx.getStorageSync('role')
    })
      db.where({
        _id:options.id
      }).get().then(res=>{
        that.setData(res.data[0])
        /* ldb.where({
          stuno:res.data[0].stu
        }).get().then(res1=>{
          that.setData({
            name:res1.data[0].name,
            grade:res1.data[0].grade
          })
        }) */
      })
      
   
        
      },
  tip: function (str) {
    const that = this
    that.setData({
      tip_content: str,
      tip: true
    })
    setTimeout(function () {
      that.setData({
        tip: false
      })
      wx.navigateBack({
        delta: 0,
      })
    }, 1000)
  },
  btnClick:function(e){
    const that=this
    if(that.data.status=='审批中'){
      wx.showModal({
        title: '提示',
        content:'确认撤销本次申请吗？',
        success:function(res){
          if(res.confirm){
            db.doc(that.data._id).remove({
              success:function(_res){
               that.tip('撤销成功！')
              }
            })
          }
        }
      })
    }else{
      wx.redirectTo({
        url: '/pages/apply/apply',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})